package org.andao.commons.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.andao.commons.cache.CacheAppManager;
import org.andao.security.model.User;


/**
 * 全局uri.如文件存储目录，WEB root url等.
 *
 * @author Seewo Software - Marco.hu(huzhiguo@cvte.cn)
 *
 */
public class FileUriContext {

	private static FileUriContext context;
	
	private FileUriContext(){}
			
	public static FileUriContext instance(){
		if(null == context){
			context = new FileUriContext();
		}
		return context;
	}
	
	/**
	 * web context url,eg. http://172.18.80.113:8081/iclass
	 * @param request
	 * @return
	 */
	public String getWebContextUrl(HttpServletRequest request){
		return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() + request.getContextPath();
	}
	
	/**
	 * web root url,eg. http://172.18.80.113:8081
	 * @param request
	 * @return
	 */
	public String getWebRootUrl(HttpServletRequest request){
	    return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
	}
	
	/**
	 * download root url,eg. http://172.18.80.113:8081/edu/attachs/
	 * @param request
	 * @return
	 */
	public String getDownloadUrl(HttpServletRequest request){
		return getWebRootUrl(request) + CacheAppManager.getSysConfigurationByCode("web.uploadfile.rooturl").getParamValue();
	} 
	
	/**
	 * server store root path,eg. /opt/seewo/data/
	 * @return
	 */
	public String getServerStoreRootPath(){
		return CacheAppManager.getSysConfigurationByCode("web.uploadfile.rootpath").getParamValue();
	}
	
	/**
	 * user at server store root path,eg. /opt/seewo/data/users
	 * @return
	 */
	public String getUserServerStoreRootPath(){		
		return ifEmptyCreateFolder(getServerStoreRootPath()+"users");
	}
	
	/**
	 * get server store temp path,eg. /opt/seewo/data/temp
	 * @return
	 */
	public String getServerStoreTempPath(){
		return ifEmptyCreateFolder(getServerStoreRootPath()+"temp");
	}
	/**
	 * get server store temp path,eg. /opt/seewo/data/
	 * @return
	 */
	public String getServerStorePathByType(String username,String type){
		return ifEmptyCreateFolder(getServerStoreRootPath()+"users/"+username+"/"+type);
	}

	
	/*~~~~~~~~~~ seewoclass~~~~~~~~~~~~~~~~~*/
	/**
	 * 获取某用户文件的绝对路径的父目录
	 * @param username
	 * @return
	 */
	public String getAttachPath(String username) {
		return ifEmptyCreateFolder(getServerStoreRootPath() + "users" + File.separator +username+ File.separator + "attachs"+File.separator);
	}

	/**
	 * 根据文件ID，获取某用户具体文件的绝对路径
	 * @param username
	 * @param teacherCourseFileId
	 * @return
	 */
	public String getAttachUnZipAbsolutePath(String username,String teacherCourseFileId) {
		return ifEmptyCreateFolder(getAttachPath(username)+teacherCourseFileId);
	}
	
	/**
	 * 获取文件解压的相对路径
	 * @param username
	 * @param teacherCourseFileId
	 * @return
	 */
	public String getAttachUnZipRelativePath(String username,String teacherCourseFileId) {
		return "users"+File.separator+username+File.separator+"attachs"+File.separator+teacherCourseFileId;
	}

	/**
	 * 获取具体一条课堂记录的文件相对路径
	 * @param lessonId
	 * @param teacher
	 * @param recordId
	 * @return
	 */
	public String getRecordModulePath(String lessonId, User teacher,String recordId) {
		return "/users/"+teacher.getUsername()+"/lesson/"+lessonId+"/teacher/file/"+recordId+".txt";
	}
	
	/**
	 * 获取课堂记录相对路径
	 * @param lessonId
	 * @param teacher
	 * @return
	 */
	public String getRecordParentPath(String lessonId, User teacher) {
		return "/users/"+teacher.getUsername()+"/lesson/"+lessonId+"/teacher/file/";
	}
	
	private String ifEmptyCreateFolder(String path){
		File f = new File(path);
		if(!f.exists()) f.mkdirs();
		return path;
	}
	/**
	 * 获取用户允许上传的文件格式
	 * @return
	 */
	public String getFileExt() {
		return CacheAppManager.getSysConfigurationByCode("web.uploadfile.accept").getParamValue();
	}
}
